package com.roamer.javase.demo.leecode;

/**
 * @author roamer
 * @version v1.0
 * @date 2021/3/16 20:18
 */
public class FindSum {

    public int[] find(int[] numbers, int sum) {
        if (numbers.length == 0) {
            return new int[]{};
        }

        boolean[] bitMap = new boolean[numbers[numbers.length - 1] + 1];
        for (int i = 0; i < numbers.length; i++) {
            bitMap[numbers[i]] = true;
        }
        int max = 0;
        int[] result = new int[2];

        for (int i = 0; i < numbers.length; i++) {
            int num = numbers[i];
            int offset = sum - num;
            if (offset >= 0 && bitMap[offset] && max > num * offset) {
                max = num * offset;
                result[0] = Math.max(num, offset);
                result[1] = Math.min(num, offset);
            }
        }
        return result;
    }
}
